import 'package:common_utils/common_utils.dart';
import 'package:erp/data/model/process_handle.dart';
import 'package:erp/main_state.dart';
import 'package:erp/widgets/form1/form_builder.dart';
import 'package:erp/widgets/my_app_bar.dart';
import 'package:erp/widgets/process_attachment_list.dart';
import 'package:erp/widgets/sale_project_detail_list.dart';
import 'package:erp/widgets/sale_project_title.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:timelines/timelines.dart';

import 'controller.dart';
import 'widgets/process_card.dart';

export 'binding.dart';

class SaleProjectProcessDetail extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var ms = Get.find<MainState>();
    var controller = Get.find<SaleProjectProcessDetailController>();
    return Scaffold(
      backgroundColor: Color(0xFFF7FAFF),
      appBar: MyAppBar(
        centerTitle: '流程详细信息',
        titleColor: Colors.black,
      ),
      body: ListView(
        children: [
          SaleProjectTile(title: '申请信息', padding: EdgeInsets.only(left: 52.w)),
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 52.w),
            child: FormBuilder(
              readOnly: true,
              formManagement: FormManagement(),
              initialValue: controller.saleProjectData,
              children: [
                OptionSelectorFormField(
                  label: '* 所属公司',
                  name: 'company',
                  options: ms.getOptions('company'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "所属公司不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 项目名称',
                  name: 'projectName',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "项目名称不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 立项编号',
                  name: 'projectNo',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "立项编号不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 销售经理(申请人)',
                  name: 'applyEmp',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "销售经理(申请人)不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 市场区域',
                  name: 'marketArea',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "市场区域不能为空";
                    }
                  },
                ),
                OptionSelectorFormField(
                  label: '* 所属大区',
                  name: 'areaDept',
                  options: ms.getOptions('area_dept'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "所属大区不能为空";
                    }
                  },
                ),
                OptionSelectorFormField(
                  label: '* 投标状态',
                  name: 'bidStatus',
                  options: ms.getOptions('bid_status'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "投标状态不能为空";
                    }
                  },
                ),
              ],
            ),
          ),
          SaleProjectTile(title: '市场立项项目信息', padding: EdgeInsets.only(left: 52.w)),
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 52.w),
            child: FormBuilder(
              readOnly: true,
              formManagement: FormManagement(),
              initialValue: controller.saleProjectData,
              children: [
                ClearableTextFormField(
                  label: '* 业主单位',
                  name: 'ownUnit',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "业主单位不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 设计单位',
                  name: 'designUnit',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "设计单位不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 总承包单位',
                  name: 'contractUnit',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "总承包单位不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 招标单位',
                  name: 'bidUnit',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "招标单位不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 建设地点',
                  name: 'buildAddress',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "招建设地点不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '* 建设阶段',
                  name: 'buildStage',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "建设阶段不能为空";
                    }
                  },
                ),
                DateSelectorFormField(
                  label: '* 启动时间',
                  name: 'beginTime',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "启动时间不能为空";
                    }
                  },
                ),
                DateSelectorFormField(
                  label: '* 预竣工时间',
                  name: 'endTime',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "预竣工时间不能为空";
                    }
                  },
                ),
                ClearableTextareaFormField(
                  label: '* 工期说明',
                  name: 'buildDesc',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "工期说明不能为空";
                    }
                  },
                ),
                OptionSelectorFormField(
                  label: '* 建设方式',
                  name: 'buildType',
                  options: ms.getOptions('build_type'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "建设方式不能为空";
                    }
                  },
                ),
                OptionSelectorFormField(
                  label: '* 项目类别',
                  name: 'projectCategory',
                  options: ms.getOptions('project_category'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "项目类别不能为空";
                    }
                  },
                ),
                OptionSelectorFormField(
                  label: '* 主要负责技术部',
                  name: 'chargeDept',
                  options: ms.getOptions('charge_dept'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "主要负责技术部不能为空";
                    }
                  },
                ),
              ],
            ),
          ),
          SaleProjectTile(title: '项目详细情况', padding: EdgeInsets.only(left: 52.w)),
          Container(
            color: Colors.white,
            padding: EdgeInsets.fromLTRB(34.w, 0, 34.w, 20.h),
            child: SaleProjectDetailList(
              saleProjectDetailList: controller.saleProjectDetails,
              readOnly: true,
              onCheck: (item) {
                controller.seeProjectDetail(item);
              },
            ),
          ),
          SaleProjectTile(title: '主要商务情况', padding: EdgeInsets.only(left: 52.w)),
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 52.w),
            child: FormBuilder(
              readOnly: true,
              formManagement: FormManagement(),
              initialValue: controller.saleProjectData,
              children: [
                OptionSelectorFormField(
                  label: '* 项目来源',
                  name: 'projectSource',
                  options: ms.getOptions('project_source'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "项目来源不能为空";
                    }
                  },
                ),
                OptionSelectorFormField(
                  label: '* 项目招投标方式',
                  name: 'bidMode',
                  options: ms.getOptions('bid_mode'),
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "项目招投标方式不能为空";
                    }
                  },
                ),
                FilterChipFormField(
                  items: controller.docListItems,
                  label: "* 需要材料",
                  name: 'docList',
                  validator: (value) {
                    if (ObjectUtil.isEmpty(value)) {
                      return "需要材料不能为空";
                    }
                  },
                ),
                ClearableTextFormField(
                  label: '其他材料',
                  name: 'otherDoc',
                ),
              ],
            ),
          ),
          SaleProjectTile(title: '流程附件', padding: EdgeInsets.only(left: 52.w)),
          Container(
            color: Colors.white,
            padding: EdgeInsets.fromLTRB(52.w, 0, 52.w, 20.h),
            child: ProcessAttachmentList(
              processFileList: controller.processAttachmentList,
            ),
          ),
          SaleProjectTile(title: '流程跟踪', padding: EdgeInsets.only(left: 52.w)),
          _ProcessTimeLine(handleList: controller.handleList),
          SizedBox(height: 20.h)
        ],
      ),
    );
  }
}

class _ProcessTimeLine extends StatelessWidget {
  final List<ProcessHandle> handleList;

  _ProcessTimeLine({required this.handleList});

  @override
  Widget build(BuildContext context) {
    var ms = Get.find<MainState>();
    return FixedTimeline.tileBuilder(
      theme: TimelineThemeData(
        indicatorTheme: IndicatorThemeData(),
        connectorTheme: ConnectorThemeData(
          thickness: 2.w,
          space: 80.w,
        ),
        nodePosition: 0.01,
        // indicatorPosition: 209.h,
      ),
      builder: TimelineTileBuilder.connected(
        itemExtent: 240.h,
        itemCount: handleList.length,
        firstConnectorBuilder: (_) => SizedBox(
          child: Center(
            child: CustomPaint(
              painter: _StartNodePainter(),
              child: Container(),
            ),
          ),
        ),
        lastConnectorBuilder: (_) => SizedBox(
          child: Center(
            child: CustomPaint(
              painter: _EndNodePainter(),
              child: Container(),
            ),
          ),
        ),
        contentsBuilder: (context, index) => ProcessTimeLineCard(
          empIdMap: ms.empIdTranslation,
          orgIdMap: ms.orgIdTranslation,
          index: index,
          processHandle: handleList[index],
        ),
        connectorBuilder: (_, index, __) => SolidLineConnector(
          color: handleList[index].handleStatus == 1 ? Color(0xff4C88FF) : Color(0xffCCDDFF),
        ),
        indicatorBuilder: (_, index) => DotIndicator(
          size: 30.w,
          color: handleList[index].handleStatus == 1 ? Color(0xff4C88FF) : Color(0xffCCDDFF),
          child: Container(
            alignment: Alignment.center,
            child: Image.asset(
              handleList[index].handleStatus == 1
                  ? "assets/images/process_track/completed.png"
                  : "assets/images/process_track/untreated.png",
              width: 16.w,
              height: 16.w,
            ),
          ),
        ),
      ),
    );
  }
}

class _StartNodePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint _paint = Paint()..color = Color(0xff4C88FF);

    canvas.drawCircle(
      Offset(size.width / 2, 0),
      5.w,
      _paint..style = PaintingStyle.fill,
    );
    // 画线
    canvas.drawLine(
        Offset(size.width / 2, 0),
        Offset(size.width / 2, size.height),
        _paint
          ..strokeWidth = 2.w
          ..style = PaintingStyle.stroke);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

class _EndNodePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint _paint = Paint()..color = Color(0xff4C88FF);
    // 画线
    canvas.drawLine(
        Offset(size.width / 2, 0),
        Offset(size.width / 2, size.height),
        _paint
          ..strokeWidth = 2.w
          ..style = PaintingStyle.stroke);

    // 画点
    canvas.drawCircle(
      Offset(size.width / 2, size.height),
      5.w,
      _paint..style = PaintingStyle.fill,
    );
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
